<template>
	<view :key="refreshKey">
		<view class="change-log">
			<view class="no-data animate__fadeInRight" v-show="noDataFlag" style="animation-duration: 2s;">
				<uv-empty text="暂无记录" mode="list"></uv-empty>
			</view>
			<view class="records animate__fadeInRight" style="animation-duration: 2s;">
				<template v-for="record in records">
					<view class="record">
						<view class="record-icon">
							<uv-tags text="盈" type="error" size="mini"></uv-tags>
						</view>
						<view class="type-time">
							<view class="type-name">{{record.projectName}}</view>
							<view class="change-time">{{record.grantTime}}</view>
						</view>
						<view class="change-amount">
							<text
								class="change-amount-value">￥{{record.income}}</text>
							<text class="change-amount-unit"></text>
						</view>
					</view>
					<uv-line></uv-line>
				</template>
				<view @click="nextPage" v-show="!noDataFlag">
					<uv-load-more margin-top="40" margin-bottom="40" :status="loadingState" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				records: [],
				pageNum: 1,
				loadingState: 'loadmore',
				pullDownRefreshFlag: false,
				noDataFlag: false,
				refreshKey:0

			}
		},
		onLoad() {
			this.findByPage();
		},
		onShow(){
			this.refreshKey += 1;
		},
		onReachBottom() {
			this.nextPage();
		},
		onPullDownRefresh() {
			this.pullDownRefreshFlag = true;
			this.refreshData();
		},
		methods: {

			moneyFormat(money, len) {
				len = len || 2
				if (!money && money !== 0)
					return ''
				if (isNaN(+money))
					return ''
				if (money === 0 || money === '0')
					return '0.' + '0'.repeat(len)
				var arr = (money + '').split('.')
				var intStr = arr[0] ? arr[0] : 0
				var floatStr = arr[1] ? arr[1] : 0
				if (floatStr === 0) {
					floatStr = '0'.repeat(len)
				} else {
					floatStr = (+('0.' + floatStr)).toFixed(len).split('.')[1]
				}
				money = (intStr + '.' + floatStr).replace(/(\d{1,3})(?=(?:\d{3})+\.)/g, `$1,`);
				return money
			},

			detailPage(record) {
				uni.navigateTo({
					url: '../balanceChangeLogDetail/balanceChangeLogDetail?id=' + record.id
				});
			},

			refreshData() {
				this.pageNum = 1;
				this.records = [];
				this.loadingState = 'loading';
				this.findByPage();
			},

			nextPage() {
				if (this.loadingState == 'nomore') {
					return;
				}
				this.pageNum = this.pageNum + 1;
				this.findByPage();
			},

			findByPage() {
				var that = this;
				var queryParam = {
					pageSize: 10,
					pageNum: that.pageNum,
				};
				that.loadingState = 'loading';
				uni.$uv.http.get('/buyInRecord/findMyFinancialIncomeRecordByPage', {
					params : queryParam
				}).then(res => {
					var newRecords = res.data.content;
					var totalPage = res.data.totalPage;
					if (that.pullDownRefreshFlag) {
						that.pullDownRefreshFlag = false;
						uni.stopPullDownRefresh();
					}
					if (newRecords.length == 0) {
						that.loadingState = 'nomore';
					}
					if (totalPage == that.pageNum) {
						that.loadingState = 'nomore';
					}
					var oldRecords = that.records;
					for (var i = 0; i < newRecords.length; i++) {
						var flag = true;
						for (var j = 0; j < oldRecords.length; j++) {
							if (newRecords[i].id == oldRecords[j].id) {
								flag = false;
								break;
							}
						}
						if (flag) {
							oldRecords.push(newRecords[i]);
						}
					}
					that.noDataFlag = that.records.length == 0;
				});
			},
		}
	}
</script>

<style>
page {
	height: auto !important;
	background: url('/static/img/my_bg.png') no-repeat;
	background-size: 100% 100%;
	background-color: #FFFFFF;
}
	.no-data {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 400rpx;
	}
	
	.change-log {
		padding-bottom: 16rpx;
	}

	.records {
		padding-left: 32rpx;
		padding-right: 32rpx;
	}

	.record {
		display: flex;
		align-items: center;
		padding-top: 32rpx;
		padding-bottom: 32rpx;
	}

	.type-name {
		font-weight: bold;
	}

	.change-time {
		font-size: small;
		color: #888;
		padding-top: 8rpx;
	}

	.type-time {
		flex: 1;
		padding-left: 16rpx;
	}

	.change-amount {}

	.change-amount-value {
		color: #E4D494;
		font-size: large;
		font-weight: bold;
		padding-right: 8rpx;
	}

	.change-amount-unit {
		font-size: 20rpx;
	}
</style>